package com.woniuxy.buyer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.buyer.entity.Message;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 站内消息Mapper
 */
@Mapper
public interface MessageMapper extends BaseMapper<Message> {

    /**
     * 查询用户的消息列表
     */
    @Select("SELECT m.*, s.nickname as sender_name, r.nickname as receiver_name " +
            "FROM messages m " +
            "LEFT JOIN users s ON m.sender_id = s.id " +
            "LEFT JOIN users r ON m.receiver_id = r.id " +
            "WHERE m.receiver_id = #{receiverId} " +
            "ORDER BY m.create_time DESC")
    List<Message> selectMessagesByReceiverId(Long receiverId);

    /**
     * 查询未读消息数量
     */
    @Select("SELECT COUNT(*) FROM messages WHERE receiver_id = #{receiverId} AND is_read = 0")
    Integer countUnreadMessages(Long receiverId);
}
